/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * MiiInstaller.h
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#ifndef _MIIINSTALLER_H_
#define _MIIINSTALLER_H_

#include "../stdafx.h"
#include "MiiTools.h"

class Mii
{
	public:
		static void Fix_DB();
		static void Fix_EmuDB();
		static int InstallNand(std::string SrcPath, int position);
		static int InstallEmu(std::string SrcPath, int position, int emuNum);
		static int NandToEmu(int nandPos, int emuPos, int emuNum);
		static int EmuToNand(int emuPos, int nandPos, int emuNum);
		static int EmuToEmu(int srcPos, int srcNum, int destPos, int destNum);
		static int DeleteNand(int position);
		static int DeleteEmu(int position, int emuNum);
		//static int DeleteAllNand(int position);
		//static int DeleteAllEmu(int position);
		
		enum
		{
			INSTALL = 0,
			DELETE,
			DELETE_ALL,			
		};
		
	protected:
		static bool Db_IsValid();
		static bool EmuDb_IsValid(std::string MiiDBPath);
		static bool NigaoeCh_IsValid();
		static void Fix_NigaoeCh();
		static const ByteArray MakeDB();
		
		static int InternalNandInstall(short type, int position, std::string SrcPath = "");	
		static int InternalEmuInstall(short type, int position, int emuNum, std::string SrcPath = "");	
		static u16 Crc16(const u8 *buf, u32 len);
		
		struct mii_t
		{
			u8 data1[2];
			u16 name[10];
			u8 data2[32];
			u16 creator[10];
		};
		
		struct mii_db
		{
			u8 header[4];
			struct mii_t mii[MII_MAX];
			u8 fill[20];
			u8 db_data[0x1D4DE];
			u16 crc;
		};
};

#endif
